//
//  RegistViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/4/22.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define kTFHeight  50

#define kTopMarginHeight   55
#define kLeftMarginHeight   20

#import "RegistViewController.h"

#import "UIButton+Countdown.h"

@interface RegistViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UITextField *codeTF;
@property (nonatomic, strong) UITextField *pswTF;
@property (nonatomic, strong) UITextField *psw2TF;
@property (nonatomic, strong) UITextField *companyNameTF;
@property (nonatomic, strong) UITextField *companyTelTF;

@property (nonatomic, strong) UIButton *registBtn;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UIImageView *photoImage;

@end

@implementation RegistViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initLoginNavBar:@"手机号注册"];
    self.view.backgroundColor = Bg_Color_F9F9F9;
    [self setUpScrollViewUI];
    [self setUpUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpScrollViewUI {
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(kNavbarAndStatusBar);
        make.bottom.equalTo(self.view.mas_bottom);
        make.width.equalTo(self.view);
    }];
}

- (void)setUpUI {
    self.photoImage = [[UIImageView alloc] init];
    self.photoImage.image = [UIImage imageNamed:@"login-6"];
    [self.scrollView addSubview:self.photoImage];
    [self.photoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollView);
        make.top.equalTo(self.scrollView.mas_top).offset(30);
        make.width.height.offset(100);
//        make.top.equalTo(self.view.mas_top).offset(10+kNavbarAndStatusBar);//80
//        make.centerX.equalTo(self.view);
    }];
    
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"login-3"];
//    bgView.contentMode = UIViewContentModeRedraw;

//    bgView.layer.borderWidth = 1;
//    bgView.layer.borderColor = Bottom_Line_Color.CGColor;
    bgView.userInteractionEnabled = YES;
    bgView.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView.mas_left).offset(10);
        make.top.equalTo(self.photoImage.mas_bottom).offset(45);
        make.centerX.equalTo(self.scrollView);
//        make.height.offset(kTFHeight*7+5);//350
    }];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.text = @"手机号码";
    phoneLabel.textColor = Text_Color_090909;
    phoneLabel.font = BHFont_Size_15;
    [bgView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(kLeftMarginHeight);
        make.top.equalTo(bgView.mas_top).offset(kTopMarginHeight);
//        make.width.offset(65);
        make.width.offset([BHSettingUtil widthForText:@"手机号码" fontSize:15]-10);
    }];

    UIView *line = [[UIView alloc] init];
    line.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(kLeftMarginHeight);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.top.equalTo(phoneLabel.mas_bottom).offset(20);//20
    }];
    
    UILabel *codeLabel = [[UILabel alloc] init];
    codeLabel.text = @"验证码";
    codeLabel.textColor = Text_Color_090909;
    codeLabel.font = BHFont_Size_15;
    [bgView addSubview:codeLabel];
    [codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.top.equalTo(line.mas_bottom).offset(20);//20
        make.width.equalTo(phoneLabel.mas_width);
    }];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.top.equalTo(codeLabel.mas_bottom).offset(20);//20
    }];
    
    UILabel *pswLabel = [[UILabel alloc] init];
    pswLabel.text = @"密码";
    pswLabel.textColor = Text_Color_090909;
    pswLabel.font = BHFont_Size_15;
    [bgView addSubview:pswLabel];
    [pswLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.top.equalTo(line1.mas_bottom).offset(20);//20
        make.width.equalTo(phoneLabel.mas_width);
    }];
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.top.equalTo(pswLabel.mas_bottom).offset(20);//20
    }];
    
    UILabel *psw2Label = [[UILabel alloc] init];
    psw2Label.text = @"登录密码";
    psw2Label.textColor = Text_Color_090909;
    psw2Label.font = BHFont_Size_15;
    [bgView addSubview:psw2Label];
    [psw2Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.top.equalTo(line2.mas_bottom).offset(20);//20
        make.width.equalTo(phoneLabel.mas_width);
    }];
    
    UIView *line3 = [[UIView alloc] init];
    line3.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.top.equalTo(psw2Label.mas_bottom).offset(20);//20
    }];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"企业名称";
    nameLabel.textColor = Text_Color_090909;
    nameLabel.font = BHFont_Size_15;
    [bgView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.top.equalTo(line3.mas_bottom).offset(20);//20
        make.width.equalTo(phoneLabel.mas_width);
    }];
    
    UIView *line4 = [[UIView alloc] init];
    line4.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line4];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.top.equalTo(nameLabel.mas_bottom).offset(20);//20
    }];
    
    UILabel *telLabel = [[UILabel alloc] init];
    telLabel.text = @"企业电话";
    telLabel.textColor = Text_Color_090909;
    telLabel.font = BHFont_Size_15;
    [bgView addSubview:telLabel];
    [telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.top.equalTo(line4.mas_bottom).offset(20);//20
        make.width.equalTo(phoneLabel.mas_width);
        make.bottom.equalTo(bgView.mas_bottom).offset(-kTopMarginHeight);//20
    }];
    
    self.phoneTF = [[UITextField alloc] init];
    self.phoneTF.placeholder = @"请输入手机号";
    self.phoneTF.font = BHFont_Size_15;
    self.phoneTF.keyboardType = UIKeyboardTypePhonePad;
    [bgView addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_right).offset(0);//25
        make.right.equalTo(line.mas_right);
        make.centerY.equalTo(phoneLabel);
        make.height.offset(kTFHeight);
    }];
    
    self.codeTF = [[UITextField alloc] init];
    self.codeTF.placeholder = @"请输入验证码";
    self.codeTF.font = BHFont_Size_15;
    [bgView addSubview:self.codeTF];
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right).offset(-85+5);
        make.centerY.equalTo(codeLabel);
        make.height.offset(kTFHeight);
    }];
    
    self.codeBtn = [[UIButton alloc] init];
    [self.codeBtn setBackgroundColor:Bg_Color_333333];
    [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:Text_Color_FFFFFF forState:UIControlStateNormal];
    [self.codeBtn addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.codeBtn.titleLabel.font = BHFont_Size_12;
    [bgView addSubview:self.codeBtn];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-15);
        make.centerY.equalTo(codeLabel);
        make.width.offset(85);
        make.height.offset(30);
    }];
    
    self.pswTF = [[UITextField alloc] init];
    self.pswTF.placeholder = @"请输入6-12位字母和数字组合";
    self.pswTF.font = BHFont_Size_15;
    self.pswTF.secureTextEntry = YES;
    [bgView addSubview:self.pswTF];
    [self.pswTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right);
        make.centerY.equalTo(pswLabel);
        make.height.offset(kTFHeight);
    }];
    
    self.psw2TF = [[UITextField alloc] init];
    self.psw2TF.placeholder = @"请输入6-12位字母和数字组合";
    self.psw2TF.font = BHFont_Size_15;
    self.psw2TF.secureTextEntry = YES;
    [bgView addSubview:self.psw2TF];
    [self.psw2TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right);
        make.centerY.equalTo(psw2Label);
        make.height.offset(kTFHeight);
    }];
    
    self.companyNameTF = [[UITextField alloc] init];
    self.companyNameTF.placeholder = @"请输入企业名称";
    self.companyNameTF.font = BHFont_Size_15;
    [bgView addSubview:self.companyNameTF];
    [self.companyNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right);
        make.centerY.equalTo(nameLabel);
        make.height.offset(kTFHeight);
    }];
    
    self.companyTelTF = [[UITextField alloc] init];
    self.companyTelTF.placeholder = @"请输入企业电话";
    self.companyTelTF.font = BHFont_Size_15;
    self.companyTelTF.keyboardType = UIKeyboardTypePhonePad;
    [bgView addSubview:self.companyTelTF];
    [self.companyTelTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right);
        make.centerY.equalTo(telLabel);
        make.height.offset(kTFHeight);
    }];

    self.registBtn = [[UIButton alloc] init];
    [self.registBtn setBackgroundColor:Bg_Color_333333];
    [self.registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.registBtn setTitleColor:Text_Color_FFFFFF forState:UIControlStateNormal];
    [self.registBtn addTarget:self action:@selector(registAction) forControlEvents:UIControlEventTouchUpInside];
    self.registBtn.titleLabel.font = BHFont_Size_17;
    self.registBtn.layer.masksToBounds = YES;
    self.registBtn.layer.cornerRadius = 10;
    [self.scrollView addSubview:self.registBtn];
    [self.registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView.mas_left).offset(10);
        make.top.equalTo(bgView.mas_bottom).offset(80);
        make.centerX.equalTo(self.scrollView);
        make.height.offset(55);
        
        make.bottom.equalTo(self.scrollView.mas_bottom).offset(-70);
    }];
}

#pragma 按钮事件
//获取验证码
- (void)codeButtonClick:(UIButton *)sender {
    if ([BHSettingUtil dataAndStringIsNull:self.phoneTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入手机号"];
    }else {
        [sender startWithTime:120 title:@"获取验证码" countDownTitle:@"已发送" mainColor:[UIColor colorWithHexString:@"#6F6F6F"] countColor:[UIColor colorWithHexString:@"#B9B9B9"]];
        self.phoneTF.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(120 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.phoneTF.userInteractionEnabled = YES;
        });
        
        [self requestPostSendCode];
    }
}

- (void)registAction {
    if ([BHSettingUtil dataAndStringIsNull:self.phoneTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入手机号"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.codeTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入验证码"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.pswTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入密码"];
    }else if (![BHSettingUtil validatePassword:self.pswTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入密码(6-12字母和数字组成)"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.psw2TF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请确认密码"];
    }else if (![self.pswTF.text isEqualToString:self.psw2TF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"两次输入的密码不同"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.companyNameTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入企业名称"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.companyTelTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入企业电话"];
    }else {
        [self requestPostRegister];
    }
}

#pragma 接口
- (void)requestPostSendCode {
    [[BHAlertUtil alertManager]showLoading:nil];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.phoneTF.text forKey:@"telno"];
    [PPHTTPRequest requestPostSendCode:^(id response, BOOL success) {
        [[BHAlertUtil alertManager]hiddenLoading];
        if (success) {
            [[BHAlertUtil alertManager]showPromptInfo:@"发送成功"];
        }else {
            self.phoneTF.userInteractionEnabled = YES;
            [self.codeBtn stopWithButtonmainColor:[UIColor colorWithHexString:@"#6F6F6F"] title:@"获取验证码"];
        }
    } params:params];
}

- (void)requestPostRegister {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.phoneTF.text forKey:@"telno"];
    [params setObject:self.pswTF.text forKey:@"password"];
    [params setObject:self.codeTF.text forKey:@"smscode"];
    [params setObject:self.companyNameTF.text forKey:@"company_name"];
    [params setObject:self.companyTelTF.text forKey:@"phone"];
    [PPHTTPRequest requestPostRegister:^(id response, BOOL success) {
        if (success) {
//            self.userModel = [SGUserModel yy_modelWithJSON:response];
//            self.userModel.UserPhone = self.phoneTextField.text;
//            self.userModel.UserPas = self.passwordTextField.text;
//            [SGUserInfo saveUserInfo:self.userModel];
//            self.block == nil ? : self.block();
            [[BHAlertUtil alertManager]showPromptInfo:@"注册成功"];
//            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:RegisterProtocolTypeSuccess],BHLoginRegister_Key, nil];
//            [[NSNotificationCenter defaultCenter] postNotificationName:BHLoginRegister_Key object:nil userInfo:dic];
//            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }
//        self.phoneTextField.userInteractionEnabled = YES;
//        [self.codeButton stopWithButtonmainColor:[UIColor colorWithHexString:@"#6F6F6F"] title:@"获取验证码"];
        
        [self.navigationController popViewControllerAnimated:YES];
    } params:params];
}

@end
